<?php

namespace Trawind\Events\Subscribers\Inventory\Fulfillment;

use Trawind\Events\Events\Inventory\Fulfillment\ClosePickEvent;
use Trawind\Events\Events\Inventory\Fulfillment\DeleteFulfillmentEvent;
use Trawind\Events\Events\Inventory\Fulfillment\DeletePickEvent;
use Trawind\Events\Events\Inventory\Fulfillment\DeleteWaveEvent;
use Trawind\Events\Events\Inventory\Fulfillment\ExecWaveEvent;
use Trawind\Events\Events\Inventory\Fulfillment\StoreFulfillmentEvent;
use Trawind\Events\Events\Inventory\Fulfillment\StoreWaveEvent;
use Trawind\Events\Events\Inventory\Fulfillment\UpdateWaveEvent;
use Trawind\Events\Events\Purchase\ClosePurchaseReturnEvent;
use Trawind\Extension\Models\Deploy\BusinessType;

class FulfillmentNoticeSubscriber
{
    private $businessType = \Trawind\Basics\Enums\BusinessType::INVENTORY_FULFILLMENT_NOTICE;

    public function storeWave($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, StoreWaveEvent::class);
        }
    }

    public function updateWave($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, UpdateWaveEvent::class);
        }
    }

    public function deleteWave($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, DeleteWaveEvent::class);
        }
    }

    public function execWave($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, ExecWaveEvent::class);
        }
    }

    public function closePick($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, ClosePickEvent::class);
        }
    }

    public function deletePick($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, DeletePickEvent::class);
        }
    }

    public function storeFulfillment($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, StoreFulfillmentEvent::class);
        }
    }

    public function deleteFulfillment($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, DeleteFulfillmentEvent::class);
        }
    }

    public function closePurchaseReturn($event)
    {
        $repository = BusinessType::getEloquentById($this->businessType);
        $repository->callback($event->payload, ClosePurchaseReturnEvent::class);
    }


    /**
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            StoreWaveEvent::class,
            static::class . '@storeWave'
        );

        $events->listen(
            UpdateWaveEvent::class,
            static::class . '@updateWave'
        );

        $events->listen(
            DeleteWaveEvent::class,
            static::class . '@deleteWave'
        );

        $events->listen(
            ExecWaveEvent::class,
            static::class . '@execWave'
        );

        $events->listen(
            ClosePickEvent::class,
            static::class . '@closePick'
        );

        $events->listen(
            DeletePickEvent::class,
            static::class . '@deletePick'
        );

        $events->listen(
            StoreFulfillmentEvent::class,
            static::class . '@storeFulfillment'
        );

        $events->listen(
            DeleteFulfillmentEvent::class,
            static::class . '@deleteFulfillment'
        );

        $events->listen(
            ClosePurchaseReturnEvent::class,
            static::class . '@closePurchaseReturn'
        );
    }
}
